package com.kt.uokos.yimo.wx.service.impl;


import com.kt.uokos.yimo.wx.dao.Address;
import com.kt.uokos.yimo.wx.example.AddressExample;
import com.kt.uokos.yimo.wx.mapper.AddressMapper;
import com.kt.uokos.yimo.wx.service.AddressService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {

    @Resource
    private AddressMapper addressMapper;


    @Override
    public int addAddress(Address address) {
        address.setAddTime(LocalDateTime.now());
        address.setUpdateTime(LocalDateTime.now());
        return addressMapper.insertSelective(address);
    }

    @Override
    public int updateAddress(Address address) {
        address.setUpdateTime(LocalDateTime.now());
        return addressMapper.updateByPrimaryKeySelective(address);
    }

    @Override
    public void resetDefaultAddress(Integer userId) {

        Address address = new Address();
        address.setIsDefault(false);
        address.setUpdateTime(LocalDateTime.now());
        AddressExample example = new AddressExample();
        example.or().andUserIdEqualTo(userId).andDeletedEqualTo(false);
        addressMapper.updateByExampleSelective(address, example);

    }

    @Override
    public Address queryAddress(Integer userId, Integer id) {
        AddressExample example = new AddressExample();
        example.or().andIdEqualTo(id).andUserIdEqualTo(userId).andDeletedEqualTo(false);
        return addressMapper.selectOneByExample(example);
    }

    @Override
    public List<Address> queryByUid(Integer uid) {
        AddressExample example = new AddressExample();
        example.or().andUserIdEqualTo(uid).andDeletedEqualTo(false);
        return addressMapper.selectByExample(example);
    }

    @Override
    public Address query(Integer userId, Integer id) {
        AddressExample example = new AddressExample();
        example.or().andIdEqualTo(id).andUserIdEqualTo(userId).andDeletedEqualTo(false);
        return addressMapper.selectOneByExample(example);
    }
}
